home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unix / unz50p1.zoo / unzip.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  60KB  |  1,541 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unzip.c
  4.  
  5.   UnZip - a zipfile extraction utility.  See below for make instructions, or
  6.   read the comments in Makefile and the various Contents files for more de-
  7.   tailed explanations.  To report a bug, send a *complete* description to
  8.   zip-bugs@cs.ucla.edu; include machine type, operating system and version,
  9.   compiler and version, and reasonably detailed error messages or problem
  10.   report.  To join Info-ZIP, send a message to info-zip-request@cs.ucla.edu.
  11.  
  12.   UnZip 5.x is a greatly expanded and partially rewritten successor to 4.x,
  13.   which in turn was almost a complete rewrite of version 3.x.  For a detailed
  14.   revision history, see UnzpHist.zip at Info-ZIP headquarters (below).  For a 
  15.   (partial) list of the many (near infinite) contributors, see "CONTRIBS" in
  16.   the UnZip source distribution.
  17.  
  18.   ---------------------------------------------------------------------------
  19.  
  20.   To compile (partial instructions):
  21.  
  22.      under Unix (cc):  make <system name>
  23.        (type "make list" for a list of valid names, or read Makefile for 
  24.        details.  "make unzip" works for most systems.  If you have a NEW
  25.        system, not covered by any of the existing targets, send FULL infor-
  26.        mation--hardware, OS, versions, etc.--to zip-bugs@cs.ucla.edu)
  27.  
  28.      under MS-DOS (MSC, Turbo C, or Borland C++):  use the makefiles or
  29.        project files in the MSDOS sub-archive; edit or otherwise modify
  30.        as appropriate.  For MSC, use NMAKE.
  31.  
  32.      under MS Windows 3.x:  get wunz12sr.{zip | zoo | whatever} and use
  33.        the included makefile
  34.  
  35.      under OS/2 (MSC, gcc, IBM C Set/2, Watcom C):  make -f makefile.os2
  36.        (from OS2 sub-archive; for MSC, use NMAKE)
  37.  
  38.      under VMS (VAX C or GNU C):  @make_unzip_vaxc  or  @make_unzip_gcc
  39.        (from VMS sub-archive; can also use MMS or MAKE/VMS; see VMS.notes)
  40.  
  41.      under Macintosh OS:  Double click on unzip.make.  Press <Command>-M.
  42.        (from MAC sub-archive)
  43.  
  44.      under Windows NT:  use makefile.nt (from NT sub-archive)
  45.  
  46.      under AmigaDOS:  try one of the makefiles in the AMIGA sub-archive;
  47.        may need some work yet...
  48.  
  49.      under Atari TOS:  needs considerable work yet...
  50.  
  51.   ---------------------------------------------------------------------------
  52.  
  53.   Version:  unz50p1.{tar.Z | zip | zoo} for Unix, VMS, OS/2, MS-DOS, Windows,
  54.               Windows NT, Macintosh and Amiga.  Decryption requires sources
  55.               in zcrypt19.zip, and Windows (not NT) support requires sources
  56.               in wunz12sr.zip.  See accompanying file "Where" in the main
  57.               source distribution for ftp, uucp and mail-server sites.
  58.   Copyrights:  see accompanying file "COPYING" in UnZip source distribution.
  59.  
  60.   ---------------------------------------------------------------------------*/
  61.  
  62.  
  63.  
  64.  
  65.  
  66. #include "unzip.h"               /* includes, defines, and macros */
  67. #ifdef MSWIN
  68. #  include "wizunzip.h"          /* see History.500 for version history */
  69. #endif
  70.  
  71. #define VERSION  "5.0p1 of 12 January 1993"
  72. /* #define VERSION  "v5.0p BETA of 8-21-92" */   /* internal beta level */
  73. #define PAKFIX   /* temporary(?) solution to PAK-created zipfiles */
  74.  
  75.  
  76.  
  77.  
  78.  
  79. /**********************/
  80. /*  Global Variables  */
  81. /**********************/
  82.  
  83. int aflag=0;          /* -a: do ASCII to EBCDIC translation, or CR-LF  */
  84.                       /*     to CR or LF conversion of extracted files */
  85. /* int bflag=0; RESERVED for -b: extract as binary */
  86. int cflag=0;          /* -c: output to stdout */
  87. int fflag=0;          /* -f: "freshen" (extract only newer files) */
  88. int jflag=0;          /* -j: junk pathnames */
  89. int overwrite_none=0; /* -n: never overwrite files (no prompting) */
  90. int overwrite_all=0;  /* -o: OK to overwrite files without prompting */
  91. int force_flag=0;     /* (shares -o for now): force to override errors, etc. */
  92. int quietflg=0;       /* -q: produce a lot less output */
  93. #ifdef DOS_OS2
  94.    int sflag=1;       /* -s: allow spaces (blanks) in filenames */
  95. #endif /* DOS_OS2 */
  96. int tflag=0;          /* -t: test */
  97. int uflag=0;          /* -u: "update" (extract only newer & brand-new files) */
  98. static int U_flag=0;  /* -U: leave filenames in upper or mixed case */
  99. static int vflag=0;   /* -v: view directory (only used in unzip.c) */
  100. int V_flag=0;         /* -V: don't strip VMS version numbers */
  101. #ifdef VMS
  102.    int secinf=0;      /* -X: keep owner/protection */
  103. #endif /* VMS */
  104. int zflag=0;          /* -z: display only the archive comment */
  105. int process_all_files=0;
  106.  
  107. longint csize;        /* used by list_files(), ReadByte(): must be signed */
  108. longint ucsize;       /* used by list_files(), unReduce(), explode() */
  109.  
  110. char *fnames[2] = {"*", NULL};   /* default filenames vector */
  111. char **fnv = fnames;
  112. char sig[5];
  113. char answerbuf[10];
  114.  
  115. min_info info[DIR_BLKSIZ], *pInfo=info;
  116.  
  117. #ifdef OS2
  118.    int longname;              /* used in extract.c, mapname.c and file_io.c */
  119.    char longfilename[FILNAMSIZ];
  120. #endif /* OS2 */
  121.  
  122. #ifdef CRYPT
  123.    char *key = (char *)NULL;  /* password with which to decrypt data, or NULL */
  124. #endif /* CRYPT */
  125.  
  126. /*---------------------------------------------------------------------------
  127.     unShrink/unReduce/explode/inflate working storage and globals:
  128.   ---------------------------------------------------------------------------*/
  129.  
  130. union work area;              /* see unzip.h for the definition of work */
  131. ULONG crc32val;
  132.  
  133. UWORD mask_bits[] = {
  134.     0x0000,
  135.     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  136.     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  137. };
  138.  
  139. /*---------------------------------------------------------------------------
  140.     Input file variables:
  141.   ---------------------------------------------------------------------------*/
  142.  
  143. byte *inbuf, *inptr;     /* input buffer (any size is legal) and pointer */
  144. int incnt;
  145.  
  146. ULONG bitbuf;
  147. int bits_left;
  148. boolean zipeof;
  149.  
  150. int zipfd;               /* zipfile file handle */
  151. #ifdef MSWIN
  152.    char *zipfn;
  153. #else
  154.    char zipfn[FILNAMSIZ];
  155. #endif
  156.  
  157. char local_hdr_sig[5] = "\120";    /* remaining signature bytes come later   */
  158. char central_hdr_sig[5] = "\120";  /*  (must initialize at runtime so unzip  */
  159. char end_central_sig[5] = "\120";  /*  executable won't look like a zipfile) */
  160. /* char extd_local_sig[5] = "\120";  NOT USED YET */
  161.  
  162. cdir_file_hdr crec;      /* used in unzip.c, extract.c, misc.c */
  163. local_file_hdr lrec;     /* used in unzip.c, extract.c */
  164. ecdir_rec ecrec;         /* used in unzip.c, extract.c */
  165. struct stat statbuf;     /* used by main(), mapped_name(), check_for_newer() */
  166.  
  167. longint extra_bytes = 0;        /* used in unzip.c, misc.c */
  168. longint cur_zipfile_bufstart;   /* extract_or_test_files, readbuf, ReadByte */
  169.   
  170. #ifdef MACOS
  171.    short  gnVRefNum;
  172.    long  glDirID;
  173.    OSType  gostCreator;
  174.    OSType  gostType;
  175.    boolean  fMacZipped;
  176.    boolean  macflag;
  177.    CursHandle  rghCursor[4];    /* status cursors */
  178.    short  giCursor = 0;
  179. #endif
  180.  
  181. /*---------------------------------------------------------------------------
  182.     Output stream variables:
  183.   ---------------------------------------------------------------------------*/
  184.  
  185. byte *outbuf;                   /* buffer for rle look-back */
  186. byte *outptr;
  187. #ifdef MSWIN
  188.    byte __far *outout;
  189.    char *filename;
  190. #else /* !MSWIN */
  191.    byte *outout;                /* scratch pad for ASCII-native trans */
  192.    char filename[FILNAMSIZ];
  193. #endif /* ?MSWIN */
  194. byte *extra_field = (byte *)NULL;  /* used by VMS, Mac and OS/2 versions */
  195. longint outpos;                 /* absolute position in outfile */
  196. int outcnt;                     /* current position in outbuf */
  197. int outfd;
  198. int mem_mode = 0;
  199. int disk_full;
  200.  
  201. /*---------------------------------------------------------------------------
  202.     unzip.c static global variables (visible only within this file):
  203.   ---------------------------------------------------------------------------*/
  204.  
  205. static byte *hold;
  206. static char unkn[10];
  207. static longint ziplen;
  208. static UWORD methnum;
  209.  
  210. /*---------------------------------------------------------------------------
  211.     unzip.c repeated error messages (we use all of these at least twice)
  212.   --------------------------------------------------------